package com.jingan.nursinghome.regulatory.dialog3;

import android.content.DialogInterface;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.adapter.MicrometerTableAdapter;
import com.jingan.nursinghome.regulatory.adapter.MicrometerTableAdapter2;
import com.jingan.nursinghome.regulatory.adapter.MicrometerTableAdapter3;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog2.CommitDailog2;
import com.jingan.nursinghome.regulatory.dialog2.XuantiDialog2;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.MySpacesItemDecoration2;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.AlertDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/12.
 */

public class MicrometerTableOSDialog3 extends BaseDialog {

    private MicrometerTableOSDialog3(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_micrometertablleos3, false, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
        BaseApplication.getInstance().setStatusBarDrak(window, true);
        initUI();
        adapterUI();
    }

    public static MicrometerTableOSDialog3 micrometerTableOSDialog;

    public static MicrometerTableOSDialog3 getInstance(BaseActivity baseActivity) {
        if (isFinishing(micrometerTableOSDialog)) {
            micrometerTableOSDialog = new MicrometerTableOSDialog3(baseActivity);
        } else {
            micrometerTableOSDialog.show();
        }
        return micrometerTableOSDialog;
    }

    MyActionBar myActionBar;

    TextView txt_comit;

    RecyclerView recyclerView;

    public String tihao = "";

    void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "专项表评分系统");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                onBack();
            }
        });
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    onBack();
                    return true;//记得返回true。表示已经处理。
                }
                return false;//返回键以外交给系统自行处理。不可以屏蔽，不然输入法键盘的按键可能无效。如删除键
            }
        });
        txt_comit = (TextView) findViewById(R.id.txt_comit);
        txt_comit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                tihao = "";
                for (int i = 0; i < MicrometerTable.getInstance().topicsZX.size(); i++) {
                    String num = i + 1 + "";
                    String c = MicrometerTable.getInstance().topicsZX.get(i).getRemark().trim();
                    if (c == null || c.equals("")) {
                        if (tihao.equals("")) {
                            tihao = num;
                        } else {
                            tihao = tihao + "," + num;
                        }
                    }
                }

                //提交
                if (MicrometerTable.getInstance().topicsZX.size() > 0) {
                    CommitDailog3.getInstance(baseActivity, micrometerTableOSDialog);
                }
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity) {
            @Override
            public boolean canScrollVertically() {
                //return canScrollVertically;
                return true;
            }
        };
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                if (itemPosition == 0) {
                    outRect.top = ProportionUtils.getInstance().adapterInt(28);
                } else {
                    outRect.top = ProportionUtils.getInstance().adapterInt(1);
                }
            }
        });

    }

    LinearLayoutManager linearLayoutManager;

    AlertDialog alertDialog;

    void onBack() {
        alertDialog = AlertDialog.getInstance(baseActivity, false, true).setMession("是否退出本次评分?");
        alertDialog.setPositive("是", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                alertDialog.dismiss();
            }
        });
    }

    void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_micrometertableos_constraintLayout));
        ProportionUtils.getInstance().adapterTextView(txt_comit);
    }

    @Override
    protected void listener() {
        recyclerView.setAdapter(new MicrometerTableAdapter3(baseActivity));
    }

    //更新
    public  void updateAdapter(){
        recyclerView.setAdapter(new MicrometerTableAdapter3(baseActivity));
    }

    @Override
    protected void recycleView() {

    }
}
